package com.example.carapplication.activitys;

import java.util.ArrayList;
import java.util.List;
import com.example.carapplication.R;
import com.example.carapplication.entry.LoginActivity;

import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class LeadActivity extends Activity {
	private List<View> viewList;
	private ViewPager viewpager1;
	private Button leadbutton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lead);
		leadbutton = (Button) findViewById(R.id.leadbutton);
		viewList = new ArrayList<View>();
		View view1 = View.inflate(this, R.layout.view1, null);
		View view2 = View.inflate(this, R.layout.view2, null);
		View view3 = View.inflate(this, R.layout.view3, null);
		viewList.add(view1);
		viewList.add(view2);
		viewList.add(view3);
		viewpager1 = (ViewPager) findViewById(R.id.viewpager);
		MypagerAdapter adapter = new MypagerAdapter(viewList);
		viewpager1.setAdapter(adapter);
		leadbutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initEvent();
			}

			private void initEvent() {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(LeadActivity.this, LoginActivity.class);
				startActivity(i);
			}
		});
		viewpager1.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					leadbutton.setVisibility(View.GONE);
					break;
				case 1:
					leadbutton.setVisibility(View.GONE);
					break;
				case 2:
					leadbutton.setVisibility(View.VISIBLE);

					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public class MypagerAdapter extends PagerAdapter {
		private List<View> viewList;

		public MypagerAdapter(List<View> viewList) {
			// TODO Auto-generated constructor stub
			this.viewList = viewList;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			container.addView(viewList.get(position));
			return viewList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView(viewList.get(position));
		}
	}
}
